//
//  QBWTLeftViewList.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/18.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "QBWTLeftViewList.h"
#import "SelectedFenZuAlertView.h"
#define CELLH 90*fh
#define GAODU 55*fh
@interface QBWTLeftViewList (){
    UIButton *fenzuButton;
    NSMutableArray *_dataArr;
}
@property (nonatomic, strong) SelectedFenZuAlertView *fenZuAlertView;
@property (nonatomic, strong) UIView *fgxView;
@property (nonatomic, copy) NSString *selectId;

@end

@implementation QBWTLeftViewList


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[WTFDTLCell class] forCellReuseIdentifier:@"WTFDTLCell"];
    [self.tableView registerClass:[QBWTRightFenZuCell class] forCellReuseIdentifier:@"QBWTRightFenZuCell"];

    
    
}

- (void)chuanRuShuJuShuXinViewData:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    [self.tableView reloadData];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataArr[section] count];
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 50;
    }else{
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return GAODU;
    }else{
        return CELLH;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        WTFDTLCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WTFDTLCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[WTFDTLCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WTFDTLCell"];
        }
        __weak typeof(self) weakSelf = self;
        cell.indexPath = indexPath;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        SYWenTiFuDaoModel *wtfd = _dataArr[indexPath.section][indexPath.row];
        cell.mainBlock = ^(NSInteger index) {
            if (index == 1) { //分组
                [weakSelf requestDataSort];
                weakSelf.selectId = wtfd.questionId;
            } else {//删除
                [weakSelf requestDeleteId:wtfd.questionId];
            }
        };
        [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:wtfd.headIng] placeholderImage:[UIImage imageNamed:@""]];
        cell.nameLabel.text = wtfd.name;
        cell.timeLabel.text = wtfd.createDate;
        cell.detailLabel.text = wtfd.content;
        if ([wtfd.isAnswer isEqualToString:@"0"]) {
            cell.doteImageView.hidden = NO;
        } else {
            cell.doteImageView.hidden = YES;
        }
        
        
         return cell;
    }else{
        QBWTRightFenZuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QBWTRightFenZuCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[QBWTRightFenZuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"QBWTRightFenZuCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        SYWenTiFenZuModel *wtfz = _dataArr[indexPath.section][indexPath.row];
        cell.nameLabel.text = wtfz.sortName;
        cell.numberLabel.text = [NSString stringWithFormat:@"数量：%@",wtfz.sortNums];
        
         return cell;
    }
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {//分组
        SYWenTiFenZuModel *wtfz = _dataArr[indexPath.section][indexPath.row];
        if (self.block) {
            self.block(wtfz.sortId);
        }
        
    } else {//未分组
        SYWenTiFuDaoModel *wtfd = _dataArr[indexPath.section][indexPath.row];
        if (self.xsBlock) {
            self.xsBlock(wtfd.questionId);
        }
    }
}

//请求分组
- (void)requestDataSort {//self.mainBlock(1);
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI fd_requestFenZuQuestionSortKeyWord:@"" success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [weakSelf alertFenZuSelectedViewListArr:listArr];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        
    }];
}

- (void)alertFenZuSelectedViewListArr:(NSMutableArray *)listArr {
    _fenZuAlertView = [[SelectedFenZuAlertView alloc]init];
    _fenZuAlertView.dataArray = listArr;
    _fenZuAlertView.preferredContentSize = CGSizeMake(360, 300);
    _fenZuAlertView.view.backgroundColor = [UIColor whiteColor];
    _fenZuAlertView.popoverPresentationController.sourceView = self.view;
    _fenZuAlertView .modalPresentationStyle = UIModalPresentationPopover;
    _fenZuAlertView.popoverPresentationController.sourceView = self.view;
    _fenZuAlertView.popoverPresentationController.sourceRect = CGRectMake(350, 180, 0, 0);
    _fenZuAlertView.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    [self presentViewController:_fenZuAlertView animated:YES completion:nil];
    __weak typeof(self) weakSelf = self;
    _fenZuAlertView.block = ^(NSInteger index) {
        //得到选择的当前班级
        [weakSelf requestDataFenZuId:index];
    };
}

//分组
- (void)requestDataFenZuId:(NSInteger)index {
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI fd_requestQuestionSortQuestionId:self.selectId sortId:SJStringWithFormat(index) oldSortId:@"" success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:@"添加分组成功"];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.message];
    }];
    
}




- (void)requestDeleteId:(NSString *)proId {
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI fd_requestQuestionDeleteQuestionId:proId success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [MBProgressHUD zr_showError:@"删除成功"];
        if (self.wtBlock) {
            self.wtBlock(1);
        }
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [MBProgressHUD zr_showError:error.message];
    }];
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}


// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}



// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    if (section == 1) {
        _headerSectionOne = [[QBWTFenLeiCell alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
        _headerSectionOne.backgroundColor = [UIColor whiteColor];
        _headerSectionOne.lineView.hidden = NO;
        return _headerSectionOne;
    }else if (section == 0){
        return self.headerSectionTwo;
    }
    return view;
}
- (UIView *)headerSectionTwo{
    if (!_headerSectionTwo) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0, self.tableView.frame.size.width, 55)];
        view.backgroundColor = [UIColor whiteColor];
        fenzuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        fenzuButton.frame = CGRectMake(self.tableView.frame.size.width/2-85, 8, 170, 40);
        [fenzuButton setTitle:@"管理分组" forState:UIControlStateNormal];
        fenzuButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [fenzuButton setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        fenzuButton.layer.cornerRadius = 5;
        fenzuButton.layer.masksToBounds = YES;
        [fenzuButton setBackgroundImage:[UIImage imageWithColor:CONTENTBACKCOLOR forSize:CGSizeMake(200, 200)] forState:UIControlStateNormal];
        [fenzuButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:fenzuButton];
        
        UIView *fgxView = [[UIView alloc] init];
        fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
        [view addSubview:fgxView];
        [fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(view);
            make.height.mas_equalTo(1);
        }];
        _headerSectionTwo = view;
    }
    return _headerSectionTwo;
}
- (void)confirm:(UIButton *)sender{
    
    self.fenZuGuanLiBlock();
}

- (UIView *)fgxView {
    if (!_fgxView) {
        _fgxView = [[UIView alloc] init];
        _fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    }
    return _fgxView;
}


@end
